package com.lm.common.core.util;

import org.apache.commons.codec.binary.Base64;

import java.io.UnsupportedEncodingException;

/**
 * Base64加密解密
 *
 * @author lm
 * @date 2019/9/17 14:57
 */
public class Base64Util {

    public static String encode(byte[] binaryData) throws UnsupportedEncodingException {
        return new String(Base64.encodeBase64(binaryData), "UTF-8");
    }

    public static byte[] decode(String base64String) throws UnsupportedEncodingException {
        return Base64.decodeBase64(base64String.getBytes("UTF-8"));
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        String para = "{\"IdList1\": 1,\"IdList2\": [1,2,3,4,5,6,18],\"IdList3\": [1,2]}";
        String data = Base64Util.encode(para.getBytes());
        System.out.println("加密前：" + data);

        byte[] byteArray = Base64Util.decode(data);
        System.out.println("解密后：" + new String(byteArray));
    }

}
